package cn.itcast.bos.web.action.page;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;

@Controller
@ParentPackage(value = "json-default")
@Namespace("/")
@Scope("prototype")
public class StandardPageAction extends ActionSupport {
    //属性驱动
    private  int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }
    @Autowired
    private StandardService standardService;
    //分页列表查询
    @Action(value = "standard_pageQuery",results = {@Result(name = "success",type = "json")})
    public String pageQuery(){
        PageRequest pageable = new PageRequest(page - 1, rows);
        Page<Standard> pageData=standardService.findPageData(pageable);
        HashMap<String, Object> result = new HashMap<>();
        result.put("total",pageData.getTotalElements());
        result.put("rows",pageData.getContent());
        ActionContext.getContext().getValueStack().push(result);

        return SUCCESS;
    }
    @Action(value = "standard_findAll",results = {@Result(name = "success",type = "json")})
    public String findAll(){
        List<Standard> standards=standardService.findAll();
        ActionContext.getContext().getValueStack().push(standards);
        return SUCCESS;
    }
}
